package cm.cool.web.util.tokencache;

import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;

public class TokenCacheMap {
	
	 private static HashMap<String, String> tokenMap=new HashMap<String, String>();
	
	 private static ReentrantLock lock = new ReentrantLock();
	/**
	 * 获取指定Key对应的Token
	 * @param key
	 * @return
	 */
	public static  String getTokenList(String key){
		return tokenMap.get(key);
	}
	
	/**
	 * 添加一个token信息
	 * @param key
	 * @param list
	 * @return
	 */
	public static boolean addTokenList(String key,String token){
		//加锁
		lock.lock();
		try {
			tokenMap.put(key, token);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}finally {
			//释放锁
			lock.unlock();
		}
		
	}
	
	/**
	 * 删除map中的token信息
	 * @param key
	 * @return
	 */
	public static Boolean deleteTokenList(String key){
		
		//加锁
		lock.lock();
		
		try {
			tokenMap.remove(key);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		
		}finally {
			//释放锁
			lock.unlock();
		}
		
	}
	
}
